//
//  MemoryViewController.m
//  phoneTool
//
//  Created by HappyMilk 02 on 2017/8/1.
//  Copyright © 2017年 tomi. All rights reserved.
//

#import "MemoryViewController.h"
#import "LoginHeader.h"
#import <sys/param.h>
#import <sys/mount.h>

@interface MemoryViewController ()
@property (weak, nonatomic) IBOutlet UIView *view_nav;
@property (weak, nonatomic) IBOutlet UILabel *lab_allMemory;
@property (weak, nonatomic) IBOutlet UILabel *lab_user;
@property (weak, nonatomic) IBOutlet UILabel *lab_other;


@property (weak, nonatomic) IBOutlet UIView *view_memory;
@property (weak, nonatomic) IBOutlet UIView *view_use;
@property (weak, nonatomic) IBOutlet UIView *view_other;


@property (nonatomic ,strong) UIView *view_green;

@property (weak, nonatomic) IBOutlet UILabel *lab_using;
@property (weak, nonatomic) IBOutlet UILabel *lab_remainder;

@end

@implementation MemoryViewController

- (IBAction)popvc:(UIButton *)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _view_nav.backgroundColor = HEXCOLOR(0x9ADC8E);
    
    NSString *str1 = IS_CHINESE?@"剩余":@"Remainder";
    NSString *str2 = IS_CHINESE?@"已用":@"Using";
    _lab_using.text = str1;
    _lab_remainder.text = str2;
    
    _view_use.layer.masksToBounds = YES;
    _view_use.layer.cornerRadius = 11;
    _view_other.layer.masksToBounds = YES;
    _view_other.layer.cornerRadius = 11;
    
    _view_green = [[UIView alloc] initWithFrame:(CGRect){0,0,0,40}];
    _view_green.backgroundColor = HEXCOLOR(0x44DB5E);
    [_view_memory addSubview:_view_green];
    
    CGFloat allMemory = [self getTotalDiskSize];
    CGFloat useMemory = [self getAvailableDiskSize];
    NSLog(@"memory --- %@ ----- %@",[self fileSizeToString:[self getTotalDiskSize]],[self fileSizeToString:[self getAvailableDiskSize]]);
    
    NSString *str = IS_CHINESE?@"手机总容量":@"Mobile Phone Total Capacity";
    _lab_allMemory.text = [NSString stringWithFormat:@"%@ %@",str,[self fileSizeToString:[self getTotalDiskSize]]];
    _lab_other.text = [self fileSizeToString:[self getAvailableDiskSize]];
    _lab_user.text = [self fileSizeToString:(allMemory-useMemory)];
    
    CGFloat width = (allMemory-useMemory)/allMemory*(__kwidth-32);
    [UIView animateWithDuration:1 delay:0 options:UIViewAnimationOptionCurveEaseOut animations:^{
        _view_green.frame = (CGRect){0,0,width,40};
    } completion:^(BOOL finished) {
        
    }];
}


-(long long)getTotalDiskSize
{
    struct statfs buf;
    unsigned long long freeSpace = -1;
    if (statfs("/var", &buf) >= 0)
    {
        freeSpace = (unsigned long long)(buf.f_bsize * buf.f_blocks);
    }
    return freeSpace;
}
-(long long)getAvailableDiskSize
{
    struct statfs buf;
    unsigned long long freeSpace = -1;
    if (statfs("/var", &buf) >= 0)
    {
        freeSpace = (unsigned long long)(buf.f_bsize * buf.f_bavail);
    }
    return freeSpace;
}

-(NSString *)fileSizeToString:(unsigned long long)fileSize
{
    NSInteger KB = 1024;
    NSInteger MB = KB*KB;
    NSInteger GB = MB*KB;
    
    if (fileSize < 10)
    {
        return @"0 B";
        
    }else if (fileSize < KB)
    {
        return @"< 1 KB";
        
    }else if (fileSize < MB)
    {
        return [NSString stringWithFormat:@"%.1f KB",((CGFloat)fileSize)/KB];
        
    }else if (fileSize < GB)
    {
        return [NSString stringWithFormat:@"%.1f MB",((CGFloat)fileSize)/MB];
        
    }else
    {
        return [NSString stringWithFormat:@"%.1f GB",((CGFloat)fileSize)/GB];
    }
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
